﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SunwellRenderer.Camera;
using Librarinth;

namespace SunwellRenderer.Rendering
{
	public struct RendererData
	{
		public int ShaderIndex;
	}

	public abstract class Renderer : Module<RendererData>
	{
		public readonly RenderingModule Parent;
		public Renderer()
		{
			Parent	= SunwellMain.Instance.Renderer;
		}

		protected abstract IEnumerable<string> AllInitialShader { get; }
		protected abstract void OnLoadContent();
		public void LoadContent()
		{
			foreach(var shaderName in AllInitialShader)
			{
				int i;
				Alloc(out i);
				Parent.AllShaders.GetIndex(shaderName,out Datas[i].ShaderIndex);
			}

			OnLoadContent();
		}

		protected override void Alloc(ref RendererData data)
		{ data.ShaderIndex	= -1; }
		protected override void Free(ref RendererData data)
		{ data.ShaderIndex	= -1; }

		protected abstract void Begin(GraphicsDevice device);
		protected abstract void End(GraphicsDevice device);


		public void Draw(GraphicsDevice device,int sceneDataIndex)
		{
			Begin(device);

			foreach(int index in this.AllIndex)
			{
				var shader	= Parent.AllShaders.Datas[Datas[index].ShaderIndex];
				if(shader.Shader != null && shader.ModelBuffer.Count > 0)
					shader.Shader.Draw(device,sceneDataIndex,shader.ModelBuffer);
			}

			End(device);
		}
	}
}